package com.fengxu.gamescript.util

import android.Manifest
import android.app.Activity
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.content.UriPermission
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.provider.DocumentsContract
import android.provider.Settings
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import com.fengxu.gamescript.service.AutoAccessibilityService
import java.net.URL


/**
 * 权限工具类
 */
object PermissionUtils {

    /***
     * 检查悬浮窗开启权限
     * @param context 上下文
     * @return  是否开启
     */
    fun checkFloatPermission(context: Context): Boolean {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return true
        return Settings.canDrawOverlays(context)
    }

    /**
     * 判断是否获取了读/写权限
     * @param context 上下文
     * @return 是否具有权限
     */
    fun checkReadWritePermission(context: Context):Boolean{
        val permissions = arrayOf<String>(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
        return permissions.all {
            ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
        }
    }

    /**
     * 判断无障碍服务是否可用
     */
    fun accessibilityIsEnabled():Boolean{
        return AutoAccessibilityService.serviceIsRunning()
    }

    /**
     * 检查用户是否打开了通知权限
     */
    fun checkCanSendNotify(ctx:Context):Boolean{
        val notificationManager  = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        return notificationManager.areNotificationsEnabled()
    }

    /**
     * 跳转至打开系统通知界面
     */
    fun openNotifySettingPage(ctx:Context){
        val intent = Intent().apply {
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
                putExtra(Settings.EXTRA_APP_PACKAGE, ctx.packageName)
                putExtra(Settings.EXTRA_CHANNEL_ID, ctx.applicationInfo.uid)
            }else{
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
                data = Uri.fromParts("package", ctx.packageName, null)
            }
        }
        ctx.startActivity(intent)
    }

    /**
     * 判断是否获取了Android/data目录的访问··权限
     */
    fun isGrantAndroidData(context: Context):Boolean{
        for (uriPermission in context.contentResolver.persistedUriPermissions) {
            if(uriPermission.uri.toString() == "content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata"){
                return true
            }
        }
        return false
    }

    /**
     * 申请Android/data目录的访问权限
     * @param requestCode 请求码
     */
    @RequiresApi(Build.VERSION_CODES.O)
    fun requestAndroidDataPermission(activity: Activity, requestCode:Int){
        val uri = Uri.parse("content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata")
        val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
        intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri)
        intent.flags = (Intent.FLAG_GRANT_READ_URI_PERMISSION
                or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                or Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
                or Intent.FLAG_GRANT_PREFIX_URI_PERMISSION)
        activity.startActivityForResult(intent, requestCode)
    }

}